<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="原编码" prop="cdCode">
        <el-input
          v-model="queryParams.cdCode"
          placeholder="请输入原编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="疏(放)水位置" prop="sfsAddress">
        <el-input
          v-model="queryParams.sfsAddress"
          placeholder="请输入疏(放)水位置"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="疏(放)目标层位" prop="sfMbcw">
        <el-input
          v-model="queryParams.sfMbcw"
          placeholder="请输入疏(放)目标层位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="疏(放)层厚度" prop="sfHd">
        <el-input
          v-model="queryParams.sfHd"
          placeholder="请输入疏(放)层厚度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="疏(放)层埋深" prop="sfMs">
        <el-input
          v-model="queryParams.sfMs"
          placeholder="请输入疏(放)层埋深"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="疏(放)孔深" prop="sfKs">
        <el-input
          v-model="queryParams.sfKs"
          placeholder="请输入疏(放)孔深"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="疏(放)孔径" prop="sfKj">
        <el-input
          v-model="queryParams.sfKj"
          placeholder="请输入疏(放)孔径"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总疏(放)水量" prop="sfsTotal">
        <el-input
          v-model="queryParams.sfsTotal"
          placeholder="请输入总疏(放)水量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['define:pumpDefine:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['define:pumpDefine:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['define:pumpDefine:remove']"
          >删除
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="pumpDefineList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="原编码" align="center" prop="cdCode"  width="150"/>
      <el-table-column label="测点编码" align="center" prop="remark"  width="150"/>
      <el-table-column
        label="疏(放)水位置"
        align="center"
        prop="sfsAddress"
        width="150"
      />
      <el-table-column label="测点单位" align="center" prop="cdUnit"  width="150"/>
      <el-table-column
        label="疏(放)目标层位"
        align="center"
        prop="sfMbcw"
        width="150"
      />
      <el-table-column
        label="疏(放)层厚度"
        align="center"
        prop="sfHd"
        width="150"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.sfHd }} <span class="price">m</span></span>
        </template>
      </el-table-column>
      <el-table-column
        label="疏(放)层埋深"
        align="center"
        prop="sfMs"
         width="150"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.sfMs }} <span class="price">m</span></span>
        </template>
      </el-table-column>
      <el-table-column
        label="疏(放)孔深"
        align="center"
        prop="sfKs"
         width="150"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.sfKs }} <span class="price">m</span></span>
        </template>
      </el-table-column>
      <el-table-column
        label="疏(放)孔径"
        align="center"
        prop="sfKj"
         width="150"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.sfKj }} <span class="price">mm</span></span>
        </template>
      </el-table-column>
      <el-table-column label="坐标X" align="center" prop="sbX"  width="150"/>
      <el-table-column label="坐标Y" align="center" prop="sbY"  width="150"/>
      <el-table-column label="坐标Z" align="center" prop="sbZ"  width="150"/>
      <el-table-column label="单孔流量" align="center" prop="dkLl"  width="150"/>
      <el-table-column
        label="总疏(放)水量"
        align="center"
        prop="sfsTotal"
         width="150"
      />
      <el-table-column
        label="数据定义时间"
        align="center"
        prop="dataTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.dataTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
         width="150"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              size="small"
              type="success"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['define:pumpDefine:edit']"
            >
            </el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['define:pumpDefine:remove']"
            >
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改疏(放)水监测基础信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col span="12">
            <el-form-item label="原编码" prop="cdCode">
              <el-input v-model="form.cdCode" placeholder="请输入原编码" />
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="测点单位" prop="cdUnit">
              <el-select
                v-model="form.cdUnit"
                placeholder="请选择测点单位"
                style="width:100%"
              >
                <el-option
                  v-for="dict in measurePointUnitList"
                  :key="dict.unitName"
                  :label="dict.unitDesc"
                  :value="dict.unitName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="疏(放)水位置" prop="sfsAddress">
              <el-input
                v-model="form.sfsAddress"
                placeholder="请输入疏(放)水位置"
              />
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="疏(放)目标层位" prop="sfMbcw">
              <el-input
                v-model="form.sfMbcw"
                placeholder="请输入疏(放)目标层位"
              />
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="疏(放)层厚度" prop="sfHd">
              <el-input
                v-model="form.sfHd"
                placeholder="请输入疏(放)层厚度"
                style="width:80%"
                @input="inputnum"
              />
              <span class="unit">m</span>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="疏(放)层埋深" prop="sfMs">
              <el-input
                v-model="form.sfMs"
                placeholder="请输入疏(放)层埋深"
                style="width:80%"
                @input="inputnum"
              />
              <span class="unit">m</span>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="疏(放)孔深" prop="sfKs">
              <el-input
                v-model="form.sfKs"
                placeholder="请输入疏(放)孔深"
                style="width:80%"
                @input="inputnum"
              />
              <span class="unit">m</span>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="疏(放)孔径" prop="sfKj">
              <el-input
                v-model="form.sfKj"
                placeholder="请输入疏(放)孔径"
                style="width:80%"
                @input="inputnum"
              />
              <span class="unit">mm</span>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="坐标X" prop="sbX">
              <el-input v-model="form.sbX" placeholder="请输入坐标X" />
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="坐标Y" prop="sbY">
              <el-input v-model="form.sbY" placeholder="请输入坐标Y" />
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="坐标Z" prop="sbZ">
              <el-input v-model="form.sbZ" placeholder="请输入坐标Z" />
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="单孔流量" prop="dkLl">
              <el-input
                v-model="form.dkLl"
                placeholder="请输入单孔流量"
                @input="inputnum"
              />
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="总疏(放)水量" prop="sfsTotal">
              <el-input
                v-model="form.sfsTotal"
                placeholder="请输入总疏(放)水量"
                @input="inputnum"
              />
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="数据定义时间" prop="dataTime">
              <el-date-picker
                clearable
                size="small"
                v-model="form.dataTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择数据定义时间"
                style="width: 100%;"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPumpDefine,
  getPumpDefine,
  delPumpDefine,
  addPumpDefine,
  updatePumpDefine
} from "@/api/define/pumpDefine";
import { listMeasurePointUnit } from "@/api/dictData/measurePointUnit";
export default {
  name: "PumpDefine",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 疏(放)水监测基础信息表格数据
      pumpDefineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 数据定义时间时间范围
      daterangeDataTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mineCode: null,

        mineName: null,

        cdCode: null,

        sfsAddress: null,

        cdUnit: null,

        sfMbcw: null,

        sfHd: null,

        sfMs: null,

        sfKs: null,

        sfKj: null,

        sbX: null,

        sbY: null,

        sbZ: null,

        dkLl: null,

        sfsTotal: null,

        dataTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cdCode: [
          { required: true, message: "原编码不能为空", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],

        sfsAddress: [
          { required: true, message: "疏(放)水位置不能为空", trigger: "blur" }
        ],

        sfMbcw: [
          { required: true, message: "疏(放)目标层位不能为空", trigger: "blur" }
        ],

        sfHd: [
          { required: true, message: "疏(放)层厚度不能为空", trigger: "blur" },
          {
            min: 1,
            max: 8,
            message: "长度在 1 到 8 个字符,小数点后面2位",
            trigger: "blur"
          }
        ],

        sfMs: [
          { required: true, message: "疏(放)层埋深不能为空", trigger: "blur" },
          {
            min: 1,
            max: 8,
            message: "长度在 1 到 8 个字符,小数点后面2位",
            trigger: "blur"
          }
        ],

        sfKs: [
          { required: true, message: "疏(放)孔深不能为空", trigger: "blur" },
          {
            min: 1,
            max: 8,
            message: "长度在 1 到 8 个字符,小数点后面2位",
            trigger: "blur"
          }
        ],

        sfKj: [
          { required: true, message: "疏(放)孔径不能为空", trigger: "blur" },
          {
            min: 1,
            max: 8,
            message: "长度在 1 到 8 个字符,小数点后面2位",
            trigger: "blur"
          }
        ],

        sbX: [{ required: true, message: "坐标X不能为空", trigger: "blur" }],

        sbY: [{ required: true, message: "坐标Y不能为空", trigger: "blur" }],

        sbZ: [{ required: true, message: "坐标Z不能为空", trigger: "blur" }],

        dkLl: [
          { required: true, message: "单孔流量不能为空", trigger: "blur" },
          {
            min: 1,
            max: 8,
            message: "长度在 1 到 8 个字符,小数点后面2位",
            trigger: "blur"
          }
        ],

        sfsTotal: [
          { required: true, message: "总疏(放)水量不能为空", trigger: "blur" },
          {
            min: 1,
            max: 8,
            message: "长度在 1 到 8 个字符,小数点后面2位",
            trigger: "blur"
          }
        ],

        dataTime: [
          { required: true, message: "数据定义时间不能为空", trigger: "blur" }
        ]
      },
      measurePointUnitList: []
    };
  },
  created() {
    this.getList();
    listMeasurePointUnit(this.queryParams).then(response => {
      this.measurePointUnitList = response.rows;
    });
  },
  methods: {
    // 限制只能在输入框中输入小数点后两位
    inputnum() {
      this.form.sfHd = this.form.sfHd.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.sfMs = this.form.sfMs.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.sfKs = this.form.sfKs.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.sfKj = this.form.sfKj.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.dkLl = this.form.dkLl.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.sfsTotal = this.form.sfsTotal.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
    },
    /** 查询疏(放)水监测基础信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDataTime && "" != this.daterangeDataTime) {
        this.queryParams.params["beginDataTime"] = this.daterangeDataTime[0];
        this.queryParams.params["endDataTime"] = this.daterangeDataTime[1];
      }
      listPumpDefine(this.queryParams).then(response => {
        this.pumpDefineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        mineCode: null,

        mineName: null,

        cdCode: null,

        sfsAddress: null,

        cdUnit: null,

        sfMbcw: null,

        sfHd: null,

        sfMs: null,

        sfKs: null,

        sfKj: null,

        sbX: null,

        sbY: null,

        sbZ: null,

        dkLl: null,

        sfsTotal: null,

        dataTime: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDataTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加疏(放)水监测基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPumpDefine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改疏(放)水监测基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePumpDefine(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPumpDefine(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除当前选中数据？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delPumpDefine(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  }
};
</script>
